<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
    <meta content=always name=referrer>
    <link rel="stylesheet" type="text/css" href="__STATIC__/css/style.css">
    <script src="__STATIC__/js/jquery-1.8.2.js"></script>
    <title>商品管理</title>
    <style>
        .wrap-goods .header .type ul{
            background-image: url(__STATIC__/img/bg7.png);
        }
    </style>
    <script>
        $(function(){
            $(".type li").click(
                    function(){
                        var a=$(".type li").index(this);
                        $(this).addClass("cur");
                        $(this).siblings().removeClass("cur");
                    }
            )
            $(".pic").click(
                    function(){
                        $(".type ul").toggle();
                    }
            );
        })
    </script>
{include file="public/href" /}
</head>
<body>
<div class="wrap-goods">
    <div class="header">
        <p>商品管理</p>
        <img src="__STATIC__/img/back.png" class="back" onclick =window.location.href="{:url('mobile/shop_auth/member_shop')}";>
        <img src="__STATIC__/img/search.png" class="search" onclick =window.location.href="{:url('mobile/shop_auth/goods_query')}";>
        <div class="type">
            <img src="__STATIC__/img/type.png" class="pic">
            <ul>
                <li {eq name="sort_type" value="sort"} class="cur" {/eq}><a href="?show={:input('show')}&sort_type=sort">默认排序</a></li>
                <li {eq name="sort_type" value="create_time"} class="cur" {/eq} ><a href="?show={:input('show')}&sort_type=create_time">添加时间</a></li>
                <li {eq name="sort_type" value="sale_number"} class="cur" {/eq}><a href="?show={:input('show')}&sort_type=sale_number">销量</a></li>
                <li {eq name="sort_type" value="inventory"} class="cur" {/eq}><a href="?show={:input('show')}&sort_type=inventory">库存</a></li>
                <li {eq name="sort_type" value="hits"} class="cur" {/eq}><a href="?show={:input('show')}&sort_type=hits">访问量</a></li>
            </ul>
        </div>
    </div>
    <div class="center">
        <div class="tablist">
            <ul>
                <li {eq name="show" value="sale"}class="cur"{/eq}><a href="javascript:jump(1);">出售中(<font id="cs">{$sale_count}</font>)</a></li>
                <li {eq name="show" value="warehouse"}class="cur"{/eq}><a href="javascript:jump(2);">仓库中(<font id="ck">{$warehouse_count}</font>)</a></li>
                <input type="hidden" id="show" value="{:input('show')}" />
                <input type="hidden" id="sort_type" value="{:input('sort_type','sort')}" />
                <input type="hidden" id="search" value="{:input('search','')}" />
            </ul>
        </div>
        <div class="tabcon"></div>
    </div>
</div>
{include file='public/src' /}
<script src="__STATIC__/js/layui/layui.js"></script>
<script>
    //类型转换
    function jump(type){
        var search = $('#search').val();
        if (search == ''){
            if (type == 1){
                window.location.href = "?show=sale";
            }else{
                window.location.href = "?show=warehouse";
            }
        }else{
            if (type == 1){
                window.location.href = "?show=sale&search="+search;
            }else{
                window.location.href = "?show=warehouse&search="+search;
            }
        }
    }
    //layer流加载
    layui.use('flow', function() {
        var $ = layui.jquery;
        var flow = layui.flow;
        var show = $('#show').val(); //当前浏览的类型
        var sort_type = $('#sort_type').val(); //当前排序的类型
        var search = $('#search').val();
        flow.load({
            elem: '.tabcon' //流加载容器
            ,isAuto:true
            ,isLazyimg:true
            ,done: function(page, next){ //执行下一页的回调
                var lis = [];
                var ids = '';
                //整个页面的id
                $("[name='goods_id']").each(function (i,o) {
                    if (ids == ''){
                        ids = $(o).val();
                    }else{
                        ids += ','+$(o).val();
                    }
                });
                page = 1;
                $.ajax({
                    url:"{:url('mobile/shop_auth/goods_manage')}",
                    dataType:'json',
                    type:'post',
                    data:{'show':show,'sort_type':sort_type,'page':page,'ids':ids,'search':search},
                    success:function(t){
                        layui.each(t.data, function(index, item){
                            var tip = '';
                            var state = '';
                            if (item.status == 3){
                                tip = '<span class="tip">违规禁售</span>';
                            }else if (item.status == 2){
                                tip = '<span class="tip">正在审核中</span>';
                            }
                            if (item.state == 1){
                                state = '<li><a href="javascript:down('+item.id+');">下架</a></li>';
                            }else if (item.state == 2){
                                state = '<li><a href="javascript:up('+item.id+','+item.status+');">上架</a></li>';
                            }
                            lis.push('<div class="cur" id="item_'+item.id+'">' +
                                    '<input type="hidden" name="goods_id" value="'+item.id+'"'+
                                    '<div class="content">' +
                                    '<div class="left">' +
                                    '<img lay-src="/'+item.thumb_img+'" ></div>' +
                                    '<div class="right">' +
                                    '<div class="title">' +
                                    '<p>'+item.title+'</p><img src="__STATIC__/img/wddp-yuiwen.png"></div>' +
                                    '<div class="price"><div>&yen;'+item.price+'</div>' +
                                    '<div><span>周周币</span><img src="__STATIC__/img/sy-qianbi.png">' +
                                    '<font class="scale">'+item.rebate_percentage+'%</font></div>' +
                                    '<input type="text" hidden id="cause_'+item.id+'" value="'+item.cause+'" />' +
                                    '</div><ul><li>已售 '+item.sale_number+' </li><li>库存 '+item.inventory+' </li>' +
                                    '<li>今日 '+item.hits+'人 </li></ul></div></div>' +
                                    '<div class="tips" id="tips_'+item.id+'" style="position: relative;margin-top: 0;">' +tip+
                                    '<ul class="list">' +state+
                                    '<li><a href="javascript:del('+item.id+','+item.status+');">删除</a></li></ul></div></div>');
                        });
                        next(lis.join(''), page < t.pages);
                    },
                    error:function () {
                        layer.open({content:'网络繁忙,请重试',skin:'msg',time:2});
                    }
                })
            }
        });
    });
    //出售中商品下架操作
    function down(id){
        var ck = $('#ck'); //仓库
        var cs = $('#cs'); //出售
        layer.open({content: '确定要下架此商品吗？',btn: ['确认', '取消']
            ,yes: function(index){
                $.ajax({
                    url:'sold_out',
                    type:'post',
                    dataType:'json',
                    data:{'id':id,'type':1},
                    success:function (t) {
                        layer.open({content:t.message,skin:'msg',time:2});
                        //移除此元素
                        if (t.code == 200){
                            $('#item_'+id).remove();
                            $('#tips_'+id).remove();
                            //仓库数量对应+1 自己-1
                            cs.text(parseInt(cs.text())-1);
                            ck.text(parseInt(ck.text())+1);
                        }

                    }
                })
                layer.close(index);
            }
        });
    }
    //仓库中商品上架操作
    function up(id,status){
        if (status == 2){
            layer.open({content:'此商品正在审核中,禁止上架',btn:'我知道了'});
            return false;
        }else if(status == 3){
            var cause_content = $('#cause_'+id).val();
            layer.open({content:'违规原因:'+cause_content,btn:'我知道了'});
            return false;
        }
        var ck = $('#ck'); //仓库
        var cs = $('#cs'); //出售
        layer.open({content: '确定要上架此商品吗？',btn: ['确认', '取消']
            ,yes: function(index){
                $.ajax({
                    url:'sold_out',
                    type:'post',
                    dataType:'json',
                    data:{'id':id,'type':2},
                    success:function (t) {
                        layer.open({content:t.message,skin:'msg',time:2});
                        //移除此元素
                        if (t.code == 200){
                            $('#item_'+id).remove();
                            $('#tips_'+id).remove();
                            //仓库数量对应+1 自己-1
                            cs.text(parseInt(cs.text())+1);
                            ck.text(parseInt(ck.text())-1);
                        }
                    }
                })
                layer.close(index);
            }
        });
    }
    //商品删除操作
    function del(id,status){
        var ck = $('#ck'); //仓库
        var cs = $('#cs'); //出售
        layer.open({content: '确定要删除此商品吗？',btn: ['确认', '取消']
            ,yes: function(index){
                $.ajax({
                    url:'destroy',
                    type:'post',
                    dataType:'json',
                    data:{'id':id},
                    success:function (t) {
                        layer.open({content:t.message,skin:'msg',time:2});
                        //移除此元素
                        if (t.code == 200){
                            $('#item_'+id).remove();
                            $('#tips_'+id).remove();
                            //数量相应-1
                            if (status == 1){
                                cs.text(parseInt(cs.text())-1);
                            }else if (status == 2){
                                ck.text(parseInt(ck.text())-1);
                            }
                        }
                    }
                });
                layer.close(index);
            }
        });
    }
</script>
</body>
</html>